package com.ruitu.health_1.ui;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.health_1.R;
import com.ruitu.health_1.ui.fragment.order.AllOrderFragment;
import com.ruitu.health_1.ui.fragment.order.CompletedFragment;
import com.ruitu.health_1.ui.fragment.order.OrderOverFragment;
import com.ruitu.health_1.ui.fragment.order.ShippedFragment;
import com.ruitu.health_1.ui.fragment.order.WaitingPayFragment;
import com.ruitu.health_1.ui.fragment.order.WaitingSendFragment;
import com.ruitu.router_module.bean.EventModel;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class OrderListActivity extends BaseActivity {
    int status;
    private ViewPager viewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleName(status);
        Arad.bus.register(this);
    }

    private void setTitleName(int position) {
        switch (position) {
            case 0:
                setHeadTitle("全部订单");
                break;
            case 1:
                setHeadTitle("待付款");
                break;
            case 2:
                setHeadTitle("待收货");
                break;
            case 3:
                setHeadTitle("待评价");
                break;
            case 5:
                setHeadTitle("已完成");
                break;
        }
    }

    private String tabTitle[] = {"全部", "待支付", "待收货", "待评价", "已完成"};
    private FragmentPagerAdapter adapter;

    @Override
    protected void initViews() {
        super.initViews();
        status = getIntent().getExtras().getInt("status");
        viewPager = findViewById(R.id.view_pager);
        tabLayout = findViewById(R.id.tab);
        adapter = new FragmentAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(fragments.length);
        viewPager.setCurrentItem(status);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                setTitleName(position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setAdapter(EventModel model) {
        if (model.getEventCode() == 2020010800) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_order_list;
    }

    private Fragment[] fragments = {AllOrderFragment.newInstance(), WaitingPayFragment.newInstance(), WaitingSendFragment.newInstance(),
             CompletedFragment.newInstance(), OrderOverFragment.newInstance()};

    class FragmentAdapter extends FragmentPagerAdapter {

        public FragmentAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Arad.bus.unregister(this);
    }
}
